* ASI 90 91 cleanup
* Unlike process91-ach.do, it keeps asicode88 and does not merge into asicode99

set more off
use "$data\asi9091.dta", clear

g year = 1990

keep year v5 v11a v3 v16 v17 v18 v19 v20 v24 v26 v28 v29 v35 v37 v38 v39 v40 v41 v50 v51 v49 v52 v66 v67 v68 v88 v57 v58 v93 v108

/* some of these may be wrong!*/

* correcting statecode ( Statecode chnaged in 1988 and then again in 1991)
* Converting it to pre-88 statecode
* distcode is asicode88

ge statecode88= v3

replace statecode88 = 41 if v3 == 2
replace statecode88 = 34 if v3 == 3
replace statecode88 = 31 if v3 == 4
replace statecode88 = 54 if v3 == 29
replace statecode88 = 51 if v3 == 5

replace statecode88 = 14 if v3 == 6
replace statecode88 = 15 if v3 == 7
replace statecode88 = 13 if v3 == 8
replace statecode88 = 53 if v3 == 9
replace statecode88 = 43 if v3 == 10
replace statecode88 = 22 if v3 == 11
replace statecode88 = 52 if v3 == 12
replace statecode88 = 35 if v3 == 13
replace statecode88 = 38 if v3 == 14
replace statecode88 = 37 if v3 == 15
replace statecode88 = 32 if v3 == 16
replace statecode88 = 12 if v3 == 17
replace statecode88 = 11 if v3 == 18
replace statecode88 = 42 if v3 == 20
replace statecode88 = 36 if v3 == 21
replace statecode88 = 21 if v3 == 22
replace statecode88 = 33 if v3 == 23
replace statecode88 = 45 if v3 == 24
replace statecode88 = 17 if v3 == 26

replace statecode88 = 54 if v3 == 33
replace statecode88 = 16 if v3 == 28
replace statecode88 = 44 if v3 == 32

drop v3
rename statecode88 statecode

rename v24 ruralurbancode

rename v5 industrycode
rename v11a multiplier
rename v16 ownershipcode
rename v17 orgcode
rename v18 managcode
rename v19 schemecode
rename v20 districtcode
rename v26 setup
rename v28 openclose
rename v29 powercode
rename v35 factories
rename v37 closingvalueland
rename v38 fixedcapitalop
rename v39 fixedcapitalcl
rename v40 landrental
rename v41 totalrent
rename v50 workers
rename v49 mandays
rename v51 workers_2
rename v88 workerswages
rename v66 totaloutput
rename v67 depreciation
rename v108 electricity
rename v57 fuel
rename v58 materials
rename v68 valueadded
rename v93 profits


drop if openclose==1


/* undoing multiplier madness*/
replace multiplier=multiplier/100

/* creating 3-dig industry code*/
ge indcode_str=string(industrycode)
replace indcode_str=substr(indcode_str, 1,3)
destring indcode_str, generate(indcode_3dig)
drop indcode_str
label var indcode_3dig "3 dig industry code"

rename indcode_3dig nic87code

/* creating a 2-digit code */
ge nic87code_str=string(nic87code)
replace nic87code_str=substr(nic87code_str, 1,2)
destring nic87code_str, generate(nic87code_2dig)
drop nic87code_str
label var nic87code_2dig "2 dig NIC87 code"

/* creating a 1-digit code */
ge nic87code_str=string(nic87code)
replace nic87code_str=substr(nic87code_str, 1,1)
destring nic87code_str, generate(nic87code_1dig)
drop nic87code_str
label var nic87code_1dig "1 dig NIC87 code"

/* price deflator*/
sort nic87code
merge nic87code using "$data\indexfinal.dta"
tab _merge
keep if _merge==3
drop _merge

g asicode88 = statecode*100 + districtcode
sort asicode88

/* generating factories variable*/
drop factories
ge factories=1
